package com.mlz.mlzmall.ui.activity.act.personal;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.ui.activity.act.base.TopTitleView;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.VolleyBaseActivity;
import com.mlz.mlzmall.ui.model.RealNameInfo;
import com.mlz.mlzmall.ui.model.RealNameSave;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.LogUtils;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;
import com.mlz.mlzmall.ui.utils.vali.IDcardValidate;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 2016/5/18.
 */
public class RealNameApprove extends VolleyBaseActivity {

    private EditText etRealName;//真实姓名
    private EditText etId;//身份证
    private Button btnRealNameSave;//保存按钮
    private String name;
    private String IdCard;
    private boolean isLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.realname_approve);//跳转到实名认证界面
        setViewContent();
        initView();
        getIdCard();
    }
    private void setViewContent() {
        TopTitleView loginTopTitle = (TopTitleView) findViewById(R.id.login_top_title);
        loginTopTitle.setLeftButtonContent(null, R.mipmap.pic_2_1_btn_back, null);
        loginTopTitle.setTitleButtonContent("实名认证", null, null);
        loginTopTitle.setOnLeftButtonClickListener(new TopTitleView.OnLeftButtonClickListener() {
            @Override
            public void onLeftBtnClick(View button) {
                finish();
                RealNameApprove.this.overridePendingTransition(R.anim.right_in,R.anim.right_out);
            }
        });
    }

    private void initView() {
        etRealName = (EditText) findViewById(R.id.et_real_name);
        etId = (EditText) findViewById(R.id.et_id);
        btnRealNameSave = (Button) findViewById(R.id.btn_real_name_save);
        btnRealNameSave.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_real_name_save:
                String name = etRealName.getText().toString();
                String IdCard = etId.getText().toString();
                if (name == null || IdCard == null || name.length() == 0 || IdCard.length() == 0) {
                    showToast("请把信息填写完整");
                } else if (!IDcardValidate.IDCardValidate(IdCard)) {
                    showToast("身份证号不正确");
                } else {
                    postIdCard(name, IdCard);
                }

                break;
            default:
                showTitle();
                break;
        }
    }

    //验证
    private void getIdCard() {
        tag = "rGet";
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.REALNAME.INFO, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(tag, jsonObject.toString());
                Gson gson = new Gson();
                RealNameInfo userInfo = gson.fromJson(jsonObject.toString(), RealNameInfo.class);
                if (userInfo.getStatus() == 0) {
                    if (userInfo.getData() != null) {
                        RealNameInfo.DataBean data = userInfo.getData();
                        etRealName.setText(data.getName());
                        etId.setText(data.getCertificateCode());
                        etRealName.setSelection(etRealName.getText().length());
                        etId.setSelection(etId.getText().length());
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(tag, error.getMessage(), error);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);

    }

    private void postIdCard(String name, String idCard) {
        //   RequestManager.cancelAll(tag);//请求界面销毁时去掉网络请求
        tag = "rPost";
        Map<String, String> param1 = new HashMap<String, String>();
        param1.put("telephone", telephone);
        param1.put("name", isNull(name));
        param1.put("certificateCode", isNull(idCard));
        JSONObject jsonObject = new JSONObject(param1);
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, URL.REALNAME.SAVE, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                RealNameSave userInfo = gson.fromJson(jsonObject.toString(), RealNameSave.class);
                Log.e(tag, jsonObject.toString());
                if (userInfo.getStatus() == 0) {
                    getIdCard();
                }
                showToast(userInfo.getMsg());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(tag, error.getMessage(), error);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }
}
